<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
<script>
    //对象转查询字符串
    function objToSearchString(obj){
        var str=''
        for(var key in obj){
            str+=`${key}=${obj[key]}&` 
        }
        return str.slice(0,str.length-1)
    }
    //封装ajax
    function ajax(method,url,params){
        return new Promise((resolve,reject)=>{
            var xhr=new XMLHttpRequest
            xhr.open(method,method=='get'? url+"?"+objToSearchString(params):url)
            xhr.onreadystatechange=function(){
                if(xhr.readyState==4){
                    resolve(JSON.parse(xhr.responseText))
                }
            }
            xhr.setRequestHeader('content-type','application/x-www-form-urlencoded')
            xhr.send(method=='get'? null:objToSearchString(params))
        })
    }
</script>
</html>